package com.hao.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.user.entity.User;
import com.hao.user.mapper.UserMapper;
import com.hao.user.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 世浩
 * @since 2023-06-06
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("getList")
    public String  getList(Model model){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        List<User> users = userMapper.selectList(wrapper);
        model.addAttribute("users",users);
        return "userList";
    }
    @GetMapping("getPage")
    public String  getPage(Model model){
        Page<User> page = new Page<>(1,2);
        userMapper.selectPage(page,null);
        List<User> users = page.getRecords();
        model.addAttribute("users",users);
        return "userList";
    }
    @GetMapping("delete/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        userMapper.deleteById(id);
        return "redirect:/user/getList";
    }
    @GetMapping("update/{id}")
    public String update(@PathVariable("id") Integer id,Model model){
        User user1 = new User();
        User user = userMapper.selectById(id);
        model.addAttribute("user",user);
        user1.setId(id);
        return "update";
    }
    @GetMapping("updateUser")
    public String updateUser(User user){
        userMapper.updateById(user);
        return "redirect:/user/getList";
    }
//    @ResponseBody
    @GetMapping("selectLike")
    public String selectLike(String name,String address,Model model){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name",name);
        wrapper.like("address",address);
        List<User> users = userMapper.selectList(wrapper);
        model.addAttribute("users",users);
        return "userList";
    }
    @GetMapping("add")
    public String add(){
        return "add";
    }
    @GetMapping("addUser")
    public String addUser(User user){
        userMapper.insert(user);
        return "redirect:/user/getList";
    }
}

